<?php
/**
 * 《领料明细》实现类
 *
 * @author 孙晓晔
 * @version $Id: Apply.class.php 839 2010-01-07 12:36:26Z xuy $
 */
require_once(dirname(__FILE__) . '/Used.class.php');

class RecordOut extends Used {
	public $tbl = Constant::tbl_wl_record_out;

	public $orderby = array(
		'-' => '请选择',
		'material' => '材料',
		'category' => '大类',
		'subclass' => '小类',

		'standard' => '规格',
			
		'price' => '单价',
			
		'emp' => '申请人',
		'1' => '创建时间',
		'2' => '更新时间',
	);
	
	public $groupby = array(
		'-' => '请选择',		
		'mold' => '产品',
		'material' => '材料',

		'status' => '状态',
		'2' => '日期',
		'3' => '月份',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		$controller->assign('t1', strtotime('-3 month'));

		$controller->display($request, 'material/out/record/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == 'category') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['category'];
		}

		if ($px == 'subclass') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['subclass'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function xls($controller, $request) {
		$this->pretreat($controller);
		
		$this->findPara($controller, $request);

		$request['tpl'] = "xls";

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();		
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$result = $db->query($sql);
		$data = array();
		while ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);

			$data[] = $row;
		}
		$controller->assign_by_ref('result', $data);
		
		$filename = sprintf("%s.xls", date("YmdHis"));
		header('Content-Disposition:application/vnd.ms-excel; filename=' . $filename);

		$controller->display($request, 'material/out/record');
		return true;
	}

	function _where($request) {
		$material_s = $this->_input($request['material_s']);

		$category_s = $request['category_s'];
		$subclass_s = $request['subclass_s'];

		$used_s = $this->_input($request['used_s']);

		$name_s = $request['name_s'];
		$standard_s = $request['standard_s'];

		$my = $this->_my();

		if (empty($my)) {
			$where = " WHERE 1=1";
		} else {
			$where = " WHERE " . $my;
		}

		if (!empty($material_s)) {
			$where .= " AND material = '" . $material_s . "'";
		}

		if (!empty($category_s) && $category_s <> '-') {
			$where .= " AND category = '" . $category_s . "'";
		}

		if (!empty($subclass_s) && $subclass_s <> '-') {
			$where .= " AND subclass = '" . $subclass_s . "'";
		}
		
		if (!empty($name_s)) {
			$where .= " AND name LIKE '%" . $name_s . "%'";
		}

		if (!empty($standard_s)) {
			$where .= " AND standard LIKE '%" . $standard_s . "%'";
		}

		if (!empty($used_s)) {
			$where .= " AND used = '" . $used_s . "'";
		}
		
		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();		
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row['u'] = $this->id(Constant::tbl_wl_used, $row['used']);				

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$row['is_delete'] = $this->_is_delete($row);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'material/out/record/list');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		require_once 'Used.class.php';
		$used = new Used();
		$controller->assign_by_ref('used', $used->_view($id));

		require_once 'Amount.class.php';
		$amount = new Amount();
		$controller->assign_by_ref('place_option', $amount->_place());

		$controller->display($request, 'material/out/record/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		$id = $request['id'];
		$material = $this->_input($request['material']);
		$place = $request['place'];
		$amount = $request['amount'];
		$content = addslashes($request['content']);

		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s WHERE used = '%s' AND material = '%s'";
		$sql = sprintf($fmt, $this->tbl, $id, $material);
		$result = $db->query($sql);
		if ($db->num_rows($result) > 0) {
			$fmt = "UPDATE %s SET amount = amount + %s, modifytime = now() WHERE used = '%s' AND material = '%s'";
			$sql = sprintf($fmt, $this->tbl, $amount, $id, $material);
		} else {
			$row = $db->fetch_array($result);
			$fmt = "INSERT INTO %s(used, mold, place, material, amount, content, emp, createtime, modifytime) VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
			$sql = sprintf($fmt, $this->tbl, $id, $row['mold'], $place, $material, $amount, $content, $_SESSION['id']);
		}
		debug($sql);
		$db->query($sql);

		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		clean($this->tbl . "_id_" . $id);
		$row = $this->id($this->tbl, $id);
		
		$row['mold_t'] = Name::mold($row['mold']);
		$row['used_t'] = Name::used($row['used']);
		
		$row['amount'] = floatval($row['amount']);

		$row['status_t'] = $this->status[$row['status']];

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'material/out/record/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$used = $this->_input($request['used']);
		$amount = $request['amount'];
		$mold = $this->_input($request['mold']);
		$content = addslashes($request['content']);

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET used = '%s', amount = '%s', mold = '%s', content = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $used, $amount, $mold, $content, $id);
		// debug($sql);
		$db->query($sql);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		require_once 'Used.class.php';
		$used = new Used();
		$controller->assign_by_ref('used', $used->_view($row['used']));

		$controller->display($request, 'material/out/record/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
		
		$row = $this->_view_t($row);
		
		return $row;
	}

	/**
	 * 领料出库
	 */
	function out($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET status = '9', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $id);
		// debug($sql);
		$db->query($sql);

		$fmt = "SELECT * FROM %s WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $id);
		// debug($sql);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			$this->_used_status($row['used'], '9');

			clean(Constant::tbl_wl_used . "_id_" . $row['used']);
			$p = $this->id(Constant::tbl_wl_used, $row['used']);
			
			$name = addslashes($row['name']);
			$standard = addslashes($row['standard']);

			$fmt = "INSERT INTO %s(material, apply, record, name, standard, unit, price, amount, applicant, emp, createtime)";
			$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now())";
			$sql = sprintf($fmt, Constant::tbl_wl_out, $row['material'], $row['used'], $row['id'], $name, $standard, $row['unit'], $row['price'], $row['amount'], $p['emp'], $_SESSION['id']);
			// debug($sql);
			$db->query($sql);

			$this->out_($row);
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	function _used_status($id, $status) {
		$db = newdb($this->dsn);
		$fmt = "SELECT * FROM %s WHERE used = '%s'";
		$sql = sprintf($fmt, Constant::tbl_wl_record_out, $id);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			if ($row['status'] <> $status) {
				return false;
			}
		}

		$fmt = "UPDATE %s SET status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, Constant::tbl_wl_used, $status, $id);
		// debug($sql);
		$db->query($sql);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function group($controller, $request) {
		$this->pretreat_group($controller);
		
		$num = array(
			'1' => '按次数',
			'2' => '按金额',
		);
		$controller->assign('num_option', $num);

		$controller->assign('t1', strtotime('-1 month'));
		// $controller->assign('t2', strtotime('+1 day'));

		$controller->display($request, 'material/out/record/group');
		return true;
	}

	function groupby($controller, $px) {
		$px = $this->groupby_aa($px, $this->groupby, 'emp');

		$controller->assign('groupby', $this->groupby[$px]);

		return $this->groupby_dd($px);
	}
	
	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function groupPost($controller, $request) {
		$this->pretreat($controller);
			
		$this->groupPara($controller, $request);

		$num_s = $request['num_s'];
		if (!empty($num_s) && $num_s == '2') {
			$fmt = "SELECT %s AS a, SUM(price*amount) AS c FROM %s a ";
		} else {
			$fmt = "SELECT %s AS a, COUNT(price) AS c FROM %s a ";
		}
		$sql = sprintf($fmt, $this->s, $this->tbl);

		$where = $this->_where($request);

		$request['sql'] = sprintf("%s %s GROUP BY %s ORDER BY %s %s", $sql, $where, $this->s, $this->orderby, $this->desc);

		$this->_group($controller, $request);

		// $controller->display($request, 'list');
		return true;
	}

	function _name($row, $px) {
		if (empty($px) || $px == '-' || $px == 'emp') {
			$row['a'] = sprintf("%s [%s]", Name::emp($row['a']), $row['a']);
		} else { 

			$row = $this->_name_t($row, $px);
		}

		return $row;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function cal($controller, $request) {
		$this->pretreat($controller);

		$controller->assign('t1', strtotime('-1 month'));

		$controller->display($request, 'material/bom/cal');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function calPost($controller, $request) {
		$this->pretreat($controller);

		$emp = $this->_input($request['emp']);

		$date1 = $request['date1'];
		$date2 = $request['date2'];

		$content = addslashes($request['content']);

		$where = $this->_cal_where($emp, $date1, $date2);

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s a, %s b SET a.category = b.category, a.subclass = b.subclass WHERE a.material = b.id %s";
		$sql = sprintf($fmt, $this->tbl, Constant::tbl_material, $where);
		debug($sql);
		$db->query($sql);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	function _cal_where($emp, $date1, $date2) {
		$where = "";
		if (!empty($emp)) {
			$where .= " AND a.emp = '" . $emp . "'";
		}

		if (!empty($date1)) {
			$where .= " AND a.createtime >= '" . $date1 . "'";
		}

		if (!empty($date2)) {
			$where .= " AND a.createtime <= '" . $date2 . "'";
		}

		return $where;
	}
}
?>
